package com.datasecops.scannerservices.db;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * HBse相关
 *
 * @param
 * @author SunLingzhi
 * @Description
 * @time 2022/10/21 18:01
 * @return
 */
public class HBase extends DBHelper {

    private static HBase instance = null;

    public static Map<String, Connection> dataSourceMap = new HashMap<>();

    public static HBase getInstance() {
        if (null == instance) {
            synchronized (HBase.class) {
                if (null == instance) {
                    instance = new HBase();
                }
            }
        }
        return instance;
    }

    public static HBase getInstance(String ip, int port, String user, String password, String Instance) {
        //初始化数据源
        if (null == instance) {
            synchronized (HBase.class) {
                if (null == instance) {
                    instance = new HBase();
                }
            }
        }

        //如果map没有这个数据源，则新建
        if (!HBase.getInstance().containsId(getSourceMapKey(ip, port, Instance))) {
            HBase.getInstance().addDataSource(ip, port, user, password, Instance);
        }
        //初始化数据源
        return instance;
    }

    /**
     * 获取连接
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:37
     */
    @Override
    public Connection getConnection(String key) {
        return dataSourceMap.get(key);
    }

    /**
     * 插入数据源
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:13
     */
    @Override
    public void addDataSource(String ip, int port, String user, String password, String Instance) {

        Configuration conf = HBaseConfiguration.create();
        //多个集群地址
        conf.set("hbase.zookeeper.quorum", ip);
        if (0 != port){
            conf.set("hbase.zookeeper.property.clientPort", port + "");
        }

        try {
            //2.创建连接
            Connection connection = ConnectionFactory.createConnection(conf);
            putHBaseSourceMap(getSourceMapKey(ip, port, Instance), connection);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("初始化创建连接池失败！");
        }
    }


    /**
     * 判断数据源是否存在
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:09
     */
    @Override
    public boolean containsId(String id) {
        return dataSourceMap.containsKey(id);
    }

    @Override
    public void putSourceMap(String sourceMapKey, DruidDataSource druidDataSource) {
    }

     /**
      * put数据
      * @Description
      * @author SunLingzhi
      * @time 2022/10/22 10:42
      * @param
      * @return
      */
    private void putHBaseSourceMap(String sourceMapKey, Connection connection) {
        dataSourceMap.put(sourceMapKey, connection);
    }

    /**
     * removeMap
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:23
     */
    @Override
    public void removeSourceMap(String sourceMapKey) {
        dataSourceMap.remove(sourceMapKey);
    }
}
